package com.aiit.carinsurance2.controller;

import com.aiit.carinsurance2.pojo.User;
import com.aiit.carinsurance2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String login(Model model){
        return "indexs";
    }
    @PostMapping("/login")
    public String dologin(@RequestParam("loginname") String loginname, @RequestParam("password")String password , HttpServletRequest request, HttpSession session){

        User u = userService.queryByLoginnameAndPassword(loginname, password);
        if(u == null){
           // request.setAttribute("msg","用户名或密码输入错误！");
            return "login";
        }else{
            session.setAttribute("user",u);
            if(u.getRole()==1){
                return "redirect:/styles";
            }else{
                return "redirect:/styles";
            }
        }
    }

    @GetMapping("/styles")
    public String getstyle(){
        return "styles";
    }
    @GetMapping("/regist")
    public String getregist(){
        return "regist";
    }

    @PostMapping("/regist")
    public String postregist(User user){
        user.setRole(2);
        int rlt=userService.regist(user);
        return "indexs";
    }


    @GetMapping("/userinfo")
    public String getusrinfo( int id , HttpServletRequest request){
        System.out.println(id);
        User user = userService.querybyid(id);
        request.setAttribute("UserList",user);
        return "userinfo";
    }
    @PostMapping("/userinfo")
    public String postuserinfo(User u,HttpServletRequest request){

    int num=-1;
    num=userService.updateuserinfo(u);
        if(num > 0) {
            request.setAttribute("msg", "更新成功");
        }else {
            request.setAttribute("msg", "更新失败");
        }
        return "redirect:/login";
    }

//    @RequestMapping("/one")
//    public String one(int id,HttpSession session){
//    User user=userService.querybyid(id);
//    System.out.println(user);
//    session.setAttribute("list",user);
//    return "userinfo";
//    }





}
